﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Data;
using KTXD.Entities;
using System.Data;

namespace UserKTXD.DCTEDU_.DanhMuc
{
    public partial class DMLoaiChuyenMuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCatalog.BindMenu(cboMenu);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            DataSet dsLoaiChuyenMuc = DataRepository.Provider.ExecuteDataSet("LoaiChuyenMuc_GetPaged", WhereClause, "IdOrder DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsLoaiChuyenMuc.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsLoaiChuyenMuc.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMLoaiChuyenMuc.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void cmdSave_Click(object sender, ImageClickEventArgs e)
        {
            bool updateStatus = false;
            try
            {//oo
                LoaiChuyenMuc entLoaiChuyenMuc = new LoaiChuyenMuc();
                entLoaiChuyenMuc.Id = Convert.ToInt32(H_CatalogId.Value);
                entLoaiChuyenMuc.TenLoaiChuyenMuc = Common.StandardString(txtTen.Text);
                entLoaiChuyenMuc.IdOrder = Convert.ToInt32(txtViTri.Text);
                entLoaiChuyenMuc.IdMenuTop = Convert.ToInt32(cboMenu.SelectedValue);
                entLoaiChuyenMuc.TrangThai = chkTrangThai.Checked;
                entLoaiChuyenMuc.StatusLoaiMenu = chkSubMenu.Checked;
                entLoaiChuyenMuc.LienKet = txtLienKet.Text;
                if (entLoaiChuyenMuc.Id == 0)
                {
                    entLoaiChuyenMuc.Id = 0;
                    updateStatus = DataRepository.LoaiChuyenMucProvider.Insert(entLoaiChuyenMuc);
                }
                else
                {
                    updateStatus = DataRepository.LoaiChuyenMucProvider.Update(entLoaiChuyenMuc);
                }
                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_CAPNHAT_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                    cmdReset_Click(sender, e);
                }
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMLoaiChuyenMuc.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_CAPNHAT_THATBAI, true);
            }
        }

        protected void cmdReset_Click(object sender, ImageClickEventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            txtTen.Text = "";
            cboMenu.SelectedValue = "0";
            chkSubMenu.Checked = false;
            chkTrangThai.Checked = false;
            txtViTri.Text = "0";
            txtLienKet.Text = "";
            H_CatalogId.Value = "0";
        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    H_CatalogId.Value = e.CommandArgument.ToString();
                    LoaiChuyenMuc entLoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                    if (entLoaiChuyenMuc != null)
                    {
                        txtTen.Text = entLoaiChuyenMuc.TenLoaiChuyenMuc;
                        txtViTri.Text = entLoaiChuyenMuc.IdOrder.ToString();
                        chkTrangThai.Checked = entLoaiChuyenMuc.TrangThai;
                        chkSubMenu.Checked = entLoaiChuyenMuc.StatusLoaiMenu;
                        cboMenu.SelectedValue = entLoaiChuyenMuc.IdMenuTop.ToString();
                        txtLienKet.Text = entLoaiChuyenMuc.LienKet;
                        Bind_Data(Pager1.CurrentIndex);
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMLoaiChuyenMuc.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
                }
            }
            if (e.CommandName == "Delete")
            {
                try
                {
                    int IdShowMenu = Convert.ToInt32(e.CommandArgument.ToString());
                    bool deleteStatus = DataRepository.LoaiChuyenMucProvider.Delete(IdShowMenu);
                    if (!deleteStatus)
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_XOA_THATBAI, true);
                    else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_XOA_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMLoaiChuyenMuc.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LOAICHUYENMUC_XOA_THATBAI, true);
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            txtTen.Text = "";
            cboMenu.SelectedValue = "0";
            chkSubMenu.Checked = false;
            chkTrangThai.Checked = false;
            txtViTri.Text = "0";
            txtLienKet.Text = "";
            H_CatalogId.Value = "0";
        }

        public string GetTenMenu(object Char)
        {
            int idMenu = Convert.ToInt32(Char.ToString());
            using (MenuTop entMenuTop = DataRepository.MenuTopProvider.GetById(idMenu))
            {
                return entMenuTop.TenMenu;
            }
        }
    }
}